<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增账号</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              tooltip: '编辑用户资料',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              tooltip: '删除此账号',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
      <template #category="{ record }">
        <Tag color="green">
          {{ record.id }}
        </Tag>
      </template>
      <template #avatar="{ record }">
        <Avatar :size="50" :src="record.avatar" />
      </template>
      <template #imgs="{ record }">
        <TableImg :size="50" :simpleShow="true" :imgList="[record.avatar]" />
      </template>
    </BasicTable>
    <AdminModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction, TableImg } from '/@/components/Table';
  import { Tag, Avatar } from 'ant-design-vue';
  import { getAdminList, AdminDel } from '/@/api/demo/auth';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AdminModal from './AdminModal.vue';

  import { columns, searchFormSchema } from './admin.data';

  export default defineComponent({
    name: 'AdminManagement',
    components: { BasicTable, AdminModal, TableAction, TableImg, Tag, Avatar },
    setup() {
      const { createConfirm } = useMessage();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '账号列表',
        api: getAdminList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        console.log(record);
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: '是否确认删除',
          onOk: function () {
            //调用删除方法
            console.log('ok');
            console.log(record);
            var ids = [record.id];
            AdminDel(ids).then(() => {
              reload();
            });
          },
          onCancel: function () {
            console.log('cancel');
          },
        });
      }

      function handleSuccess({ isUpdate, values }) {
        if (isUpdate) {
          // 演示不刷新表格直接更新内部数据。
          // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
          const result = updateTableDataRecord(values.id, values);
          console.log(result);
        } else {
          reload();
        }
      }

      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
